package com.frcdi.qms.web.controller.equipment;

import com.frcdi.qms.common.core.controller.BaseController;
import com.frcdi.qms.common.core.domain.AjaxResult;
import com.frcdi.qms.common.utils.poi.ExcelUtil;
import com.frcdi.qms.equipment.domain.EquipmentType;
import com.frcdi.qms.equipment.service.IEquipmentTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备类型Controller
 *
 * @author cell
 * @date 2023-10-10
 */
@Api(tags = "设备类型")
@RestController
@RequestMapping("/equipment/type")
public class EquipmentTypeController extends BaseController
{
    @Autowired
    private IEquipmentTypeService equipmentTypeService;

    /**
     * 查询设备类型列表
     */
    @ApiOperation("查询设备类型列表")
    @PreAuthorize("@ss.hasPermi('equipment:type:list')")
    @GetMapping("/list")
    public AjaxResult list(EquipmentType equipmentType)
    {
        List<EquipmentType> list = equipmentTypeService.selectEquipmentTypeList(equipmentType);
        return success(list);
    }

    /**
     * 导出设备类型列表
     */
    @ApiOperation("导出设备类型列表")
    @PreAuthorize("@ss.hasPermi('equipment:type:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquipmentType equipmentType)
    {
        List<EquipmentType> list = equipmentTypeService.selectEquipmentTypeList(equipmentType);
        ExcelUtil<EquipmentType> util = new ExcelUtil<EquipmentType>(EquipmentType.class);
        util.exportExcel(response, list, "设备类型数据");
    }

    /**
     * 获取设备类型详细信息
     */
    @ApiOperation("获取设备类型详细信息")
    @PreAuthorize("@ss.hasPermi('equipment:type:query')")
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") Long typeId)
    {
        return success(equipmentTypeService.selectEquipmentTypeByTypeId(typeId));
    }

    /**
     * 新增设备类型
     */
    @ApiOperation("新增设备类型")
    @PreAuthorize("@ss.hasPermi('equipment:type:add')")
    @PostMapping
    public AjaxResult add(@RequestBody EquipmentType equipmentType)
    {
        if (!equipmentTypeService.checkEquipmentTypeNameUnique(equipmentType)){
            return error(equipmentType + "'失败，名称已存在");
        }
        equipmentType.setCreateBy(getUsername());
        return toAjax(equipmentTypeService.insertEquipmentType(equipmentType));
    }

    /**
     * 修改设备类型
     */
    @ApiOperation("修改设备类型")
    @PreAuthorize("@ss.hasPermi('equipment:type:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody EquipmentType equipmentType)
    {
        if (!equipmentTypeService.checkEquipmentTypeNameUnique(equipmentType)){
        return error(equipmentType + "'失败，名称已存在");
        }
        else if (equipmentType.getParentId().equals(equipmentType.getTypeId()))
        {
            return error("修改设备类型'" + equipmentType.getEquipmentTypeName()+ "'失败，上级类型不能是自己");
        }
        equipmentType.setUpdateBy(getUsername());
        return toAjax(equipmentTypeService.updateEquipmentType(equipmentType));
    }

    /**
     * 删除设备类型
     */
    @ApiOperation("删除设备类型")
    @PreAuthorize("@ss.hasPermi('equipment:type:remove')")
	@DeleteMapping("/{typeId}")
    public AjaxResult remove(@PathVariable Long typeId)
    {
        if (equipmentTypeService.hasChildById(typeId))
        {
            return warn("存在下级,不允许删除");
        }
        return toAjax(equipmentTypeService.deleteEquipmentTypeByTypeId(typeId));
    }
}
